//
//  MPSearchView.swift
//  MPSinaWeibo
//
//  Created by DaVinci on 16/4/7.
//  Copyright © 2016年 DaVinci. All rights reserved.
//

import UIKit

class MPSearchView: UIView {
    
    
    @IBOutlet weak var rightCons: NSLayoutConstraint!
    @IBOutlet weak var searchTF: UITextField!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, self.bounds.height)
        
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        searchTF.layer.cornerRadius = 10
        searchTF.layer.masksToBounds = true
        
        searchTF.leftView?.frame = CGRectMake(0, 0, self.bounds.height, self.bounds.height)
        
        searchTF.leftView?.contentMode = .Center
        searchTF.leftViewMode = .Always
    }
    
    
    
    @IBAction func TFdidTap(sender: AnyObject) {
        
        rightCons.constant = cancelBtn.frame.width
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
        self.layoutIfNeeded()
            
        }
    }
    
    @IBAction func cancelBtnClick(sender: AnyObject) {
        
        searchTF.resignFirstResponder()
        
        rightCons.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.layoutIfNeeded()
        }
    }
    
    
    class func loadSearchView() -> MPSearchView {
    
       let search = NSBundle.mainBundle().loadNibNamed("MPSearchView", owner: nil, options: nil).last as! MPSearchView
        
        return search
    }
}
